package com.yun.newBlog.blog.controller;

import com.yun.newBlog.blog.user.MyUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.social.connect.Connection;
import org.springframework.social.connect.web.ProviderSignInUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.ServletWebRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@RestController
//@CrossOrigin
@Slf4j
public class MainController {

    @Autowired
    private ProviderSignInUtils providerSignInUtils;

    @RequestMapping("/")
    public void defaultPage(HttpServletRequest request, HttpServletResponse response) throws Exception {
        response.sendRedirect("http://www.yubuyun.com/mainPage");
    }


    @RequestMapping("/main")
    public void main(HttpServletRequest request, HttpServletResponse response) throws Exception {

        Connection<?> connection = providerSignInUtils.getConnectionFromSession(new ServletWebRequest(request));
        String nickName = "";
        String imageUrl = "";
        if(connection != null){
            nickName = connection.getDisplayName(); // 昵称
            imageUrl = connection.getImageUrl(); // 头像url
        }

        response.sendRedirect("http://www.yubuyun.com/mainPage?nickName="+nickName+"&avatar="+imageUrl);

    }
}
